package com.scuec.trainingmanagementsys.service.impl;

import com.scuec.trainingmanagementsys.entity.Project;
import com.scuec.trainingmanagementsys.entity.TeamMember;
import com.scuec.trainingmanagementsys.mapper.TeamMemberMapper;
import com.scuec.trainingmanagementsys.service.TeamMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TeamMemberServiceImpl implements TeamMemberService {

    @Autowired
    private TeamMemberMapper teamMemberMapper;

    @Override
    public boolean insertTeamMember(TeamMember teamMember) {
        // 先查询学生是否已经加入该项目
        boolean isJoined = isStudentJoinedProject(teamMember.getStudentID(), getProjectIdByTeamId(teamMember.getTeamID()));
        if (isJoined) {
            return false;
        }
        int result = teamMemberMapper.insertTeamMember(teamMember);
        return result > 0;
    }

    @Override
    public boolean updateMemberRoleToLeader(Integer teamMemberID) {
        int result = teamMemberMapper.updateMemberRoleToLeader(teamMemberID);
        return result > 0;
    }

    @Override
    public boolean isStudentJoinedProject(Integer studentId, Integer projectId) {
        int count = teamMemberMapper.isStudentJoinedProject(studentId, projectId);
        return count > 0;
    }

    private Integer getProjectIdByTeamId(Integer teamId) {
        // 这里需要根据实际情况查询数据库获取项目ID
        // 示例代码中假设存在一个 TeamMapper 用于查询团队信息
        // TeamMapper teamMapper;
        // return teamMapper.getProjectIdByTeamId(teamId);
        return null;
    }

    @Override
    public List<TeamMember> getTeamMembersByProjectId(Integer projectId) {
        return teamMemberMapper.getTeamMembersByProjectId(projectId);
    }

    @Override
    public List<Project> getJoinedProjects(Integer studentId) {
        return teamMemberMapper.getJoinedProjects(studentId);
    }

    @Override
    public List<Project> getProjectsByStudentId(Integer studentId) {
        return teamMemberMapper.getProjectsByStudentId(studentId);
    }

    @Override
    public List<Project> getProjectsWithTeacherByStudentId(Integer studentId) {
        return teamMemberMapper.getProjectsWithTeacherByStudentId(studentId);
    }

    @Override
    public List<Map<String, Object>> getStudentDistributionByProject() {
        return teamMemberMapper.getStudentDistributionByProject();
    }

    @Override
    public List<TeamMember> getTeamMembersDetailsByProjectId(Integer projectId) {
        return teamMemberMapper.getTeamMembersDetailsByProjectId(projectId);
    }

    @Override
    public boolean updateMemberRole(Integer teamMemberID, String memberRole) {
        int result = teamMemberMapper.updateMemberRole(teamMemberID, memberRole);
        return result > 0;
    }

    @Override
    public String getStudentNameByStudentId(Integer studentId) {
        return teamMemberMapper.getStudentNameByStudentId(studentId);
    }

    // TeamMemberServiceImpl.java
    @Override
    public List<TeamMember> getTeamMembersByTeamId(Integer teamId) {
        return teamMemberMapper.getTeamMembersByTeamId(teamId);
    }

    @Override
    public TeamMember getTeamMemberByStudentId(Integer studentId) {
        return teamMemberMapper.getTeamMemberByStudentId(studentId);
    }

    @Override
    public Integer getTeamIdByTeamMemberId(Integer teamMemberID) {
        TeamMember teamMember = teamMemberMapper.getTeamMemberById(teamMemberID);
        return teamMember != null ? teamMember.getTeamID() : null;
    }

    @Override
    public Integer getTeamMemberIdByStudentId(Integer studentId) {
        TeamMember teamMember = teamMemberMapper.getTeamMemberByStudentId(studentId);
        return teamMember != null ? teamMember.getTeamMemberID() : null;
    }

    @Override
    public Integer getTeamIdByStudentId(Integer studentId) {
        TeamMember teamMember = teamMemberMapper.getTeamMemberByStudentId(studentId);
        return teamMember != null ? teamMember.getTeamID() : null;
    }

    @Override
    public Integer getTeamIdByStudentAndProject(Integer studentId, Integer projectId) {
        return teamMemberMapper.getTeamIdByStudentAndProject(studentId, projectId);
    }
    @Override
    public boolean isTeamLeader(Integer teamID, Integer studentID) {
        // 假设 teamMemberMapper 中有一个方法可以根据 teamID 和 studentID 查询用户角色
        String memberRole = teamMemberMapper.getMemberRole(teamID, studentID);
        return "leader".equalsIgnoreCase(memberRole);
    }
    @Override
    public String getMemberRole(Integer teamID, Integer studentID) {
        TeamMember teamMember = teamMemberMapper.getTeamMemberByTeamIdAndStudentId(teamID, studentID);
        return teamMember != null ? teamMember.getMemberRole() : null;
    }

}